<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/12/20
 * Time: 17:08
 */

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Providers\MyJwt;
use App\Services\DepartmentService;
use Illuminate\Http\Request;
use App\Services\UserService;
use App\Services\Service;

class UserController extends Controller
{

    private $jwtCode;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $authorization = $request->header('Authorization');
        $jwt = new MyJwt();
        $jwtCode = $jwt->decode($authorization, MyJwt::ENCODE_KEY);
        if (!$jwtCode || !array_key_exists('uid', $jwtCode)) {
            $this->sendJson([], 4031, '请授权');
        }
        $this->jwtCode = $jwtCode;
    }

    /**
     * 个人资料
     */
    public function file()
    {
        $this->sendGet(function() {
            $uid = $this->jwtCode['uid'];
            $data = UserService::getById($uid);
            $this->sendJson($data);
        });
    }

    /**
     * 更新个人资料
     */
    public function update()
    {
        $this->sendPost(function() {
            $params = $this->request->get('params');
            $params['id'] = $this->jwtCode['uid'];
            UserService::save($params);
            $this->sendJson([]);
        });
    }

    /**
     * 建议
     */
    public function advice()
    {
        $this->sendGet(function() {
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $uid = $this->jwtCode['uid'];
            $data = Service::getByUid($uid, $limit, $offset);
            $this->sendDataTable($data);
        });
    }

    /**
     * 评分文件列表
     */
    public function contest()
    {
        $this->sendGet(function() {
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $data = Service::getContest($limit, $offset, 1);

            $this->sendDataTable($data);
        });
    }

    /**
     * 评分文件详情
     */
    public function contestDetail()
    {
        $this->sendGet(function() {
            $id = $this->request->get('id');
            $data = Service::getContestDetail($id);
            $this->sendJson($data);
        });
    }

    /**
     * 加分列表
     */
    public function increaseScore()
    {
        $this->sendGet(function() {
            $search = $this->request->get('search', '');
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $uid = $this->jwtCode['uid'];
            $data = UserService::getIncreaseScore($uid, $limit, $offset, $search);
            $this->sendDataTable($data);
        });
    }

    /**
     * 加分申请
     */
    public function saveIncreaseScore()
    {
        $this->sendPost(function() {
            $uid = $this->jwtCode['uid'];
            $params = $this->request->get('params');
            $params['role'] = 1;
            UserService::saveIncreaseScore($uid, $params);
            $this->sendJson([]);
        });
    }

    /**
     * 加分详情
     */
    public function increaseScoreDetail()
    {
        $this->sendGet(function() {
            $id = $this->request->get('id');
            $data = Service::getIncreaseScoreDetail($id);
            $this->sendJson($data);
        });
    }

    /**
     * 删除加分申请
     */
    public function deleteIncreaseScore()
    {
        $this->sendPost(function() {
            $id = $this->request->get('id');
            UserService::deleteIncreaseScore($id);
            $this->sendJson([]);
        });
    }

    /**
     * 星级
     */
    public function star()
    {
        $this->sendGet(function() {
            $uid = $this->jwtCode['uid'];
            $rule = UserService::getLastRule();
            $info = UserService::getById($uid);
            $advice = UserService::getLastAdvice($uid);
            $year = date('Y', time());
            $data = [
                'rule' => $rule ? $rule['content'] : '',
                'score' => $info['score'],
                'level' => UserService::getUserYearLevel($uid, $year),
                'advice' => $advice ? $advice['content'] : ''
            ];
            $this->sendJson($data);
        });
    }

    /**
     * 积分文件
     */
    public function scoreFile()
    {
        $this->sendGet(function() {
            $uid = $this->jwtCode['uid'];
            $year = $this->request->get('year');
            $data = UserService::getScoreFile($uid, $year);
            $this->sendDataTable($data);
        });
    }

    public function scoreFileDetail()
    {
        $this->sendGet(function(){
            $id = $this->request->get('id');
            $uid = $this->jwtCode['uid'];
            $data = UserService::getScoreFileDetail($id, $uid);
            $this->sendJson($data);
        });
    }

    public function saveScoreFileScore()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            $uid = $this->jwtCode['uid'];
            UserService::saveScoreFileScore($params['id'], $uid, $params['score']);
            $this->sendJson([]);
        });
    }

    /**
     *
     */
    public function contentDetail()
    {
        $this->sendGet(function() {
            $id = $this->request->get('id');

            $this->sendJson([]);
        });
    }

    /**
     * 支部列表
     */
    public function department()
    {
        $this->sendGet(function() {
            $data = DepartmentService::getSimple();
            $this->sendJson($data);
        });
    }

    public function level()
    {
        $this->sendGet(function() {
            $uid = $this->jwtCode['uid'];
            $data = UserService::getLevel($uid);
            $this->sendJson($data);
        });
    }

    public function saveLevel()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            $uid = $this->jwtCode['uid'];
            UserService::saveLevel($params, $uid);
            $this->sendJson([]);
        });
    }
}